#include "GamePlatform.h"

#include "_Global/GameCommon.h"
#include "_Global/GlobalClass.h"

#include <QDir>
#include <QStandardPaths>



_CommonData g_CommonData;



#if defined(Q_OS_WIN32)   //win

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");


    //设置和创建目录
    g_CommonData.m_strExternalDataPath = ".";
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;


    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    //MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::debug();

    return 0;
}



#elif defined(Q_OS_ANDROID)   //android

#include <QDebug>
#include <QFileInfo>
#include <QMessageBox>

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");



    bool bExternalStorage = _Platform::requestPermission(WRITE_EXTERNAL_STORAGE);
    /*bool bAccessFineLocation = _Platform::requestPermission(ACCESS_FINE_LOCATION);
    bool bAccessCoarseLocation = _Platform::requestPermission(ACCESS_COARSE_LOCATION);
    bool bCamera = _Platform::requestPermission(CAMERA);
    bool bBluetoothAdmin = _Platform::requestPermission(BLUETOOTH_ADMIN);
    bool bBluetooth = _Platform::requestPermission(BLUETOOTH);
    bool bInstallPackages = _Platform::requestPermission(INSTALL_PACKAGES);
    bool bReceiveBootCompleted = _Platform::requestPermission(RECEIVE_BOOT_COMPLETED);
    bool bRecordAudio = _Platform::requestPermission(RECORD_AUDIO);
    bool bReadContects = _Platform::requestPermission(READ_CONTACTS);
    bool bReadSMS = _Platform::requestPermission(READ_SMS);
    bool bReceiveSMS = _Platform::requestPermission(RECEIVE_SMS);
    bool bReceiveMMS = _Platform::requestPermission(RECEIVE_MMS);
    bool bReceiveWapPush = _Platform::requestPermission(RECEIVE_WAP_PUSH);*/


    //设置和创建目录
    if(bExternalStorage)
    {
        g_CommonData.m_strExternalDataPath = _Platform::getSdcardPath() + QDir::separator() + "Leamus" + QDir::separator() + "Pets";
    }
    else
    {
        g_CommonData.m_strExternalDataPath = ".";
    }
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;



    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::keepScreenWake(true);
    //_Platform::setScreenOrientation(0);



    copyFileAndSetPermissions("Game.dat", QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
    copyFileAndSetPermissions("Config.cfg", QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);


    //if(!QDir(g_CommonData.m_strExternalDataPath + QDir::separator() + "QML").exists())
    //    copyDirectoryFiles("assets:/QML",g_CommonData.m_strExternalDataPath + QDir::separator() + "QML",false);
    //if(!QDir(g_CommonData.m_strExternalDataPath + QDir::separator() + "Media").exists())
    //    copyDirectoryFiles("assets:/Media",g_CommonData.m_strExternalDataPath + QDir::separator() + "Media",false);

    QString folderPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) +
            QDir::separator() +
            "QML Projects" +
            QDir::separator() +
            "Projects" +
            QDir::separator() +
            "/Pets"
            ;
    if(!QDir(folderPath).exists())
    {
        copyDirectoryFiles("assets:/QML",folderPath,false);
        copyDirectoryFiles("assets:/Media",folderPath + QDir::separator() + "Media",false);
    }



    qDebug() << "initialization:"
             << _Platform::getSdcardPath()
             << _Platform::getNetworkState()
             << _Platform::getExternalStorageDirectory()
             << _Platform::getAndroidInfo(1)
             << _Platform::getAndroidInfo(2)
             << _Platform::getAndroidInfo(3)
             << _Platform::getAndroidInfo(4)
             << _Platform::getAndroidInfo(5)
             << _Platform::getAndroidInfo(6)
             << "android.permission.WRITE_EXTERNAL_STORAGE:"
             << bExternalStorage
                ;

    _Platform::debug();

    return 0;
}



#elif defined(Q_OS_LINUX)   //linux

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");


    //设置和创建目录
    g_CommonData.m_strExternalDataPath = ".";
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;


    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    //MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::debug();

    return 0;
}




#elif defined(Q_OS_MACOS)   //macos

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");


    //设置和创建目录
    g_CommonData.m_strExternalDataPath = ".";
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;


    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    //MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::debug();

    return 0;
}





#elif defined(Q_OS_IOS)   //ios

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");


    //设置和创建目录
    g_CommonData.m_strExternalDataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;


    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    //MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::debug();

    return 0;
}



#else

int initialization(QCoreApplication &app)
{
    app.setApplicationName("Pets");
    app.setApplicationVersion("1.0.0");
    app.setOrganizationName("Leamus");
    app.setOrganizationDomain("cn.Leamus");


    //设置和创建目录
    g_CommonData.m_strExternalDataPath = ".";
    g_CommonData.m_strLogPath = g_CommonData.m_strExternalDataPath + QDir::separator() + "Logs";

    QDir dirLogPath(g_CommonData.m_strLogPath);
    if(!dirLogPath.exists())    /**< 如果目标目录不存在，则进行创建 */
        if(!dirLogPath.mkpath(dirLogPath.absolutePath()))
            return -1;


    g_CommonData.m_pMessageHandlerInfo = MessageHandlerInfo::create();
    g_CommonData.m_pMessageHandlerInfo->m_strDatetimeFormat = QString("yyyy-MM-dd");
    g_CommonData.m_pMessageHandlerInfo->m_strWarningFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Warning_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strCriticalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Critical_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strFatalFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Fatal_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strOthersFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Others_%1.txt");


#ifdef QT_NO_DEBUG
    MessageHandlerInfo::InstallMessageHandler();
#else
    g_CommonData.m_pMessageHandlerInfo->m_strInfoFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Info_%1.txt");
    g_CommonData.m_pMessageHandlerInfo->m_strDebugFile = g_CommonData.m_strLogPath + QDir::separator() + QString("Debug_%1.txt");
    MessageHandlerInfo::InstallMessageHandler();
#endif



    _Platform::debug();

    return 0;
}

#endif
